
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.Color;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JTextField;
import javax.swing.JCheckBox;

//Patch wyamagany-log albo wiadomosc kiedy sie nie polaczymy.Sprawdzic na innym kompie.


public class BOT {

	private JFrame frame;
	private static JFrame errorframe;
	private static JTextField InputStack;
	private static BufferedReader BR = null;
	private static InputStreamReader IR;
	private static BufferedWriter OS = null;
	private static int Min;
	private static int MyStack;
	public static boolean Moved2;
	public static String[] Exchange = {"E","0","0","0","0"};
	public static String[] Hand = new String[4];
	public static int[] Flag = new int[4];
	public static int K;
	public static boolean ok = false;

	public static void main(String[] args) {
		
		 String hostname = "localhost";
		 int port = 5000;
		 Socket ClientSocket = null;   //socket of client
		 boolean connected = false;
		 K = 0;
		 
		 String Message = ""; 
		 String[] MS = null;
		 String Task = null;
		 
		// Card[0]  = new JLabel("NOTHING");
		 //PBank[0] =  new JLabel("NOPLAYER");
		// PMove[0] = new JLabel("NOTHING");
		// PLabel[0]= new JLabel("NOTHING");
		// PStake[0]= new JLabel("NOTHING");

		 
		 
			try {
				ClientSocket = new Socket(hostname,port);
				connected = true;
				IR = new InputStreamReader(ClientSocket.getInputStream());
				BR = new BufferedReader(IR);     //our BufferReader to read input stream from the server
				OS = new BufferedWriter(new OutputStreamWriter(ClientSocket.getOutputStream()));   //Output stream!! 
				
			}catch (UnknownHostException e1) {
				System.out.println("Exception: " + e1);	
			}catch (IOException e1) {
				System.out.println("IOException: " + e1);}
			
			//we established connection with bot
			 
			
//-------------------------------------LOOP-------------------------------------------------------//			
			
			
			
		//We are connected to the server. Now Client started listening to the server messages!
		//The most important loop in client,responsilbe for communication with server!
		System.out.println("Bot is listening to the Server!");
		int Moves =0;
		while(connected){
			try {
				Message = "";
				Message = BR.readLine();
				//Message Picked Up
				MS = Message.split(",");
				Task = MS[0];  //the first char in the message is responsible for telling gui what kind of info is that
				Moved2 = false;
				Message = "";
				//System.out.println(" MS wynosi : "+ MS[0]);
				//System.out.println(" I wynosi : "+ K);
				
			
			}catch (IOException e) {
				e.printStackTrace();}
				
				switch (Task){
				
				case "H":  //Hand-> H TC 9D TS AH for instance [0]-task, [1]-> 1 hand where Task array of hands
					for(int i=0;i<4;i++)
						UPDATE_HAND(MS,i);
					System.out.println("Bot received cards!");
					break;
				case "B":         //B-->basic information from 0 to 5
					System.out.println(MS[6]);
					break;
				case "C":
					break;
				case "S":
					break;
				case "M":
					System.out.println("Bot has now ready to make a turn");
					K = K +1;
					Min = Integer.parseInt(MS[2]);
					MyStack = Integer.parseInt(MS[1]);
														
					try {
						Thread.sleep(8000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					int COUNT=0;
					for(int i =0;i<=3;i++)
						Flag[i]=1;
					//ok = false;
					
					/*
					for(int i=0;i<=2;i++)
						for(int j=i+1;j<=3;j++)
							if(Flag[j]==1)
								if(Hand[i].charAt(1)==Hand[j].charAt(1)){
									COUNT=COUNT + 1;
									Flag[j]=0;} 
					if(COUNT<3)  */
						ok=true;  //if we have too many the same cards we fold
					
					System.out.println("HMMMMMMMMMMMMMMMM");
					
					if(MyStack/30<8 && ok){
						InfoServer("ALLIN");
						Moved2=true;
					}
					else if (MyStack/30>9 && ok) {
					    Min=Min+200;
					    String K = Min+"";
					    if(Moves==0)
					    	if(Min>=MyStack)
					    		InfoServer("ALLIN");
					    	else
					        	InfoServer("BR,"+K);
					    else
					    	InfoServer("CALL");
					    Moves=Moves+1;
					    Moved2=true;}
					else
						Moved2=false;
					
				
					System.out.println("DONE");
					
					if(Moved2==true){
						System.out.println("Bot MOVED");
					    }
					else{
						System.out.println("Bot decided to Fold");
						InfoServer("FOLD");
					}
					
					break;
				case "G":
					break;
				case "ST": 
					MyStack = Integer.parseInt(MS[1]);
					break;
				case "DS": //Discard function
				    //Bot has time to change card but he wont do nothing
					
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					//Discard(Exchange);
				
					break;
				case "CC":
					Message = "";
					MS[0] = "";
					Task = "";
					Moves =0;
					Moved2 = false;
					break;
				case "RC":
					break;
				case "CR":
					break;
				case "END":
					connected = false;
					break;
				}
				
		}
	
		 try {
			 OS.close();
			 BR.close();
		   	 ClientSocket.close();
		   	 System.out.println("Client Closed");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}



	

//----------------------------------------Update_METHODS---------------------------------------------//	

	
	public void Moved(boolean X){
		Moved2 = true;
	}
		

//-------------Method_For_Updating_Players_Hands--------------//

public static void UPDATE_HAND(final String[] Ms,final int i)	{  //paramters is array and what hand we are taking care of:1,2,3,4 0- is a task
	
	Hand[i]=Ms[i];
}	




/////////////////////////////////////////////////////////////////

public static void InfoServer(String S){
	try {
		OS.write(S);
		OS.newLine();
		OS.flush();	
	}catch (IOException e) {
		e.printStackTrace();}
	
	
}
/////////////////////////////////////////////////////////////

public static void Discard(String[] E){
	String S ="E";
	for(int i=1;i<5;i++)
		S=S+","+E[i];
	System.out.println("Hand request sent:"     + S);
	try {
		OS.write(S);
		OS.newLine();
		OS.flush();	
	}catch (IOException e) {
		e.printStackTrace();}
}


//////////////////////////////////////////////////
public static void Ex(final String S,final int x){
	EventQueue.invokeLater(
			new Runnable(){
				
				public void run(){
					Exchange[x] = S;
					
				}
			}
		);
}



}